<template>
  <div class="app-container">
    <Ht-search-form
      v-show="showSearch"
      :advOptions="[]"
      :dictMap="dictMap"
      :formOptions="formOptions"
      :outputQueryParams="queryParams"
      :queryParams="queryParams"
      :defaultEventKey="defaultEventKey"
      :beforeSearchCallback="false"
      @beforeSearchCallback="beforeSearchCallback"
      :clearCallBack="clearCallBack"
    >
      <template slot="endSlotOption">
        <amount-range-search label="金额区间" ref="amountRangeSearch"></amount-range-search>
      </template>
    </Ht-search-form>

    <!--    <div class="page-label-block">-->
    <!--      <span class="color-block">|</span>-->
    <!--      <span class="page-label-name">-->
    <!--      租户管理-->
    <!--      </span>-->
    <!--    </div>-->

    <ht-table
      ref="dataTable"
      :dictMap="dictMap"
      :multi-select="false"
      :opButtonList="opButtonList"
      :queryParams="queryParams"
      :reqConfig="regConfig"
      :showSerialNumber="false"
      :tableBindColumns="tableBindColumns"
      :defaultEventKey="defaultEventKey"
    >
      <template v-slot:operate>
        <el-table-column align="left" fixed="right" label="操作" width="50">
          <template slot-scope="scope">
            <ht-button-group
              :buttonList="[
                {
                  label: '明细',
                  doFunc: () => {
                    handleAction('detail', scope.row);
                  },
                },
              ]"
            />
          </template>
        </el-table-column>
      </template>
    </ht-table>

    <!-- 添加或修改弹出框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      center
      :visible.sync="open"
      v-if="open"
      append-to-body
      width="1024px"
    >
      <el-form ref="detailForm" :model="detailForm" label-width="120px">
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="批次号" prop="id">
              <el-input v-model="detailForm.id" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="工作日期" prop="workdate">
              <el-date-picker
                v-model="detailForm.workdate"
                style="width: 100%"
                type="date"
                value-format="yyyyMMdd"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="原业务类型" prop="pkgType">
              <el-select
                style="width: 100%"
                v-model="
                  detailForm.pkgType === null || detailForm.pkgType === undefined
                    ? ''
                    : detailForm.pkgType + '-' + detailForm.pkgTypeDesc
                "
                placeholder="原业务类型"
                :disabled="true"
              ></el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="发起直接参与机构" prop="direSender">
              <el-input v-model="detailForm.direSender" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="接收直接参与机构" prop="direRecver">
              <el-input v-model="detailForm.direRecver" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="回执期限" prop="pkgRtrLtd">
              <el-input v-model="detailForm.pkgRtrLtd" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="中心状态" prop="procStatus">
              <el-select style="width: 100%" v-model="detailForm.procStatus" placeholder="中心状态" :disabled="true">
                <el-option
                  v-for="dict in dictMap.PROCESSSTATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="交易总笔数" prop="dtlNum">
              <el-input v-model="detailForm.dtlNum" :disabled="true" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="交易总金额" prop="dtlAmount">
              <ht-amount-input
                v-model="detailForm.dtlAmount"
                placeholder="交易总金额"
                :disable="true"
              ></ht-amount-input>
            </el-form-item>
          </el-col>
        </el-row>

        <ht-table
          ref="regdataTable"
          :dictMap="dictMap"
          :multi-select="true"
          :opButtonList="opButtonList"
          :queryParams="queryDetailParams"
          :reqConfig="regConfigDetail"
          :showSerialNumber="false"
          :tableBindColumns="tableBindDetailColumns"
          @handleSelectionChange="handleSelectionChange"
        >
          <template v-slot:operate>
            <el-table-column align="left" fixed="right" label="操作" width="50">
              <template slot-scope="scope">
                <ht-button-group
                  :buttonList="[
                    {
                      label: '明细',
                      doFunc: () => {
                        handleAction('entrydetail', scope.row);
                      },
                    },
                  ]"
                />
              </template>
            </el-table-column>
          </template>
        </ht-table>
      </el-form>

      <div slot="footer" align="center" class="dialog-footer">
        <el-button type="primary" @click="stopPayAll" v-loading.fullscreen.lock="loading">整包止付</el-button>
        <el-button type="primary" @click="stopPayPart" v-loading.fullscreen.lock="loading">部分止付</el-button>
        <el-button type="primary" @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <stop-pay-apply-entry-detail ref="stopPayApplyEntryDetail" :dictMap="dictMap"></stop-pay-apply-entry-detail>
  </div>
</template>

<script>
import {
  apiUrlConfigEntryQuery,
  apiUrlConfigEntryDetailQuery,
  stopPayApplyEntryDetail,
  stopPayApplyEntryStopPayAll,
  stopPayApplyEntryStopPayPart,
} from "@/api/cnaps/beps/411";
import { listPageMixin } from "@/libs/listPageMixin";
import HtAmountInput from "@/views/components/HtAmountInput";
import StopPayApplyEntryDetail from "./stop_pay_apply_entry_detail";
import AmountRangeSearch from "@/views/components/AmountSearchInput";
import { amountThousandsFormatter } from "@/utils/table_col_formatter";

export default {
  name: "StopPayApplyEntry",
  mixins: [listPageMixin],
  components: { HtAmountInput, StopPayApplyEntryDetail, AmountRangeSearch },
  data() {
    let self = this;
    return {
      defaultEventKey: "defaultEventKey",
      // 查询参数
      queryParams: {
        workdate1: undefined,
        workdate2: undefined,
        idSeqNo: undefined,
        pkgNo: undefined,
        amount1: "",
        amount2: "",
      },
      loading: false,
      selectchange: [],
      queryDetailParams: {},
      entryDetailForm: {
        pkgNo: undefined,
      },

      // 搜索
      formOptions: [],
      //
      searchFormDictList: ["STOP_PAY_TYPE", "CBFLAG"],
      // 列信息
      regConfig: apiUrlConfigEntryQuery,

      regConfigDetail: apiUrlConfigEntryDetailQuery,

      tableBindColumns: [
        { prop: "id", label: "批次号", width: 180 },
        { prop: "workdate", label: "工作日期", type: "date" },
        { prop: "direSender", label: "发起直接参与机构", width: 150 },
        { prop: "direRecver", label: "接收直接参与机构", width: 150 },
        {
          prop: "pkgType",
          label: "报文类型",
          formatter(row, text, index) {
            return (
              (row["pkgType"] === null || row["pkgType"] === undefined ? "" : row["pkgType"]) +
              "-" +
              (row["pkgTypeDesc"] === null || row["pkgTypeDesc"] === undefined ? "" : row["pkgTypeDesc"])
            );
          },
          width: 240,
        },
        { prop: "pkgRtrLtd", label: "回执期限" },
        { prop: "procStatus", label: "中心状态", dictCode: "PROCESSSTATUS", width: 150 },
        { prop: "dtlNum", label: "交易总笔数", width: 150 },
        {
          prop: "dtlAmount",
          label: "交易总金额",
          type: "price",
          width: 120,
        },
      ],

      tableBindDetailColumns: [
        { prop: "id", label: "交易流水号", width: 120 },
        { prop: "pkgId", label: "报文标识号", width: 120 },
        { prop: "workdate", label: "工作日期", type: "date", width: 120 },
        {
          prop: "biztypeId",
          label: "业务类型",
          formatter(row, text, index) {
            return (
              (row["biztypeId"] === null || row["biztypeId"] === undefined ? "" : row["biztypeId"]) +
              "-" +
              (row["biztypeIdDesc"] === null || row["biztypeIdDesc"] === undefined ? "" : row["biztypeIdDesc"])
            );
          },
          width: 240,
        },
        { prop: "payerBrno", label: "付款行行号", width: 120 },
        { prop: "payeeBrno", label: "收款行行号", width: 120 },
        {
          prop: "amount",
          label: "金额",
          type: "price",
          width: 120,
        },
      ],

      dictMapAsync: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      openDetail: false,
      // 表单参数
      detailForm: {
        pkgNo: undefined,
        pkgNoDesc: undefined,
      },
      totalRecord: 0,
      totalPage: 0,
      needCount: 0,
      rules: {},
    };
  },
  mounted() {
    this.$refs.amountRangeSearch.$on('getAmountRange',this.beforeSearchCallback);
    const formOptions = [
      // 普通搜索

      {
        type: "dates",
        nameObj: {
          startTimeName: "workdate1",
          endTimeName: "workdate2",
        },
        defaultAttr: {
          size: "small",
          label: "工作日期",
          placeholder: ["请输入工作开始日期", "请输入工作结束日期"],
          format: "yyyy-MM-dd",
          valueFormat: "yyyyMMdd",
        },
      },

      {
        type: "input",
        prop: "idSeqNo",
        defaultAttr: {
          size: "small",
          label: "批次号",
          placeholder: "请输入批次号",
        },
      },

      {
        type: "dict",
        dict: "STOP_PAY_TYPE",
        prop: "pkgNo",
        defaultAttr: {
          size: "small",
          label: "报文类型",
          placeholder: "请输入报文类型",
        },
      },
    ];
    this.formOptions = formOptions;
    this.dictMapAsync = this.getDictMap();
  },
  methods: {
    /***
     * 按钮操作汇总
     */
    /***
     * 按钮操作汇总
     */
    /** 搜索按钮操作 */
    handleQuery() {
      this.$refs.dataTable.doQuery(true);
    },

    handleAction(type, row = {}) {
      switch (type) {
        case "detail":
          this.handleDetail(row);
          break;
        case "entrydetail":
          this.handleEntryDetail(row);
      }
    },

    // 关闭按钮
    cancel() {
      this.open = false;
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.title = "借记业务止付申请录入明细";
      this.detailForm = row;
      this.queryDetailParams.pkgNo = row.pkgNo;
      this.queryDetailParams.id = row.id;
      this.open = true;
    },

    handleEntryDetail(row) {
      stopPayApplyEntryDetail(row.id, this.queryDetailParams.pkgNo).then((response) => {
        this.$refs.stopPayApplyEntryDetail.show(response.result.data, response.result.pkgNo);
      });
    },

    //点击整包止付按钮
    stopPayAll: function () {
      this.loading = true;
      stopPayApplyEntryStopPayAll(this.detailForm)
        .then((response) => {
          this.msgSuccess("整包止付成功");
          this.open = false;
          this.loading = false;
          this.$nextTick(() => {
            this.$refs.dataTable.doQuery(true);
          });
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    handleSelectionChange(selection) {
      this.selectchange = selection.map((item) => item);
    },
    //部分止付
    stopPayPart() {
      if (this.selectchange.length === 0) {
        this.msgError("请勾选明细记录");
        return;
      }
      this.loading = true;
      this.detailForm.detail = this.selectchange;
      stopPayApplyEntryStopPayPart(this.detailForm)
        .then((response) => {
          this.msgSuccess("部分止付成功");
          this.selectchange = [];
          this.open = false;
          this.loading = false;
          this.$nextTick(() => {
            this.$refs.dataTable.doQuery(true);
          });
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    beforeSearchCallback() {
      // 搜索按钮之前先执行
      this.queryParams.amount1 = this.$refs.amountRangeSearch.getAmount1();
      this.queryParams.amount2 = this.$refs.amountRangeSearch.getAmount2();
    },
    clearCallBack() {
      let that = this;
      return new Promise(function (resolve, reject) {
        that.$refs.amountRangeSearch.clearAmount();
        that.queryParams.amount1 = "";
        that.queryParams.amount2 = "";
      });
    },
  },
};
</script>
